//
//  HZHuShenView.m
//  HZTC_Stock
//
//  Created by MayerF on 17/3/13.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZHuShenView.h"
#import "HZStockListTableViewCell.h"
#import "HZHeaderofSectionView.h"

#define kListCellH 59
#define kHeaderViewH 29

@interface HZHuShenView()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView      *pTableView;
@property (nonatomic, strong)NSArray          *pListData;
@property (nonatomic, strong)NSMutableArray   *pHeaderViewArray;
@end

@implementation HZHuShenView
- (NSMutableArray *)pHeaderViewArray
{
    if (!_pHeaderViewArray) {
        _pHeaderViewArray = [NSMutableArray arrayWithCapacity:5];
    }
    return _pHeaderViewArray;
}

#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self p_commonInit];
    }
    return self;
}
#pragma mark -
#pragma mark private
- (void)p_commonInit
{
    _pTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _pTableView.dataSource = self;
    _pTableView.delegate = self;
    _pTableView.bounces = NO;
    _pTableView.tableFooterView = [UIView new];
    _pTableView.backgroundColor = [UIColor clearColor];
    
    [self addSubview:_pTableView];
    
    [self p_initConstraints];
    
}

- (void)p_initConstraints
{
    [_pTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
}
#pragma mark -
#pragma mark public
- (void)updateNewData:(NSArray *)data andHeaderTitle:(NSArray *)titleArray
{
    if (data && titleArray) {
        self.pListData = data;
        [self.pHeaderViewArray removeAllObjects];
        [titleArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString *title = (NSString *)obj;
            HZHeaderofSectionView *hearder = [HZHeaderofSectionView new];
            hearder.tag = idx;
            hearder.parent = self;
            [hearder setHeaderTitle:title];
            NSArray *tempData = data[idx];
            if (tempData.count != 0) {
                hearder.isOpen = YES;
            }else {
                hearder.isOpen = NO;
            }
            [self.pHeaderViewArray addObject:hearder];
        }];
        [_pTableView reloadData];
    }
}
- (void)resetTableViewOffset
{
    _pTableView.contentOffset = CGPointZero;
}


#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.pListData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *pTempRows = self.pListData[section];
    if (pTempRows.count) {
        return pTempRows.count;
    }else{
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    tableView.separatorColor = kMYHexToRGB(0xe9e9e9, 1);
    NSArray *pTempArray = _pListData[indexPath.section];
    if (pTempArray.count == 0) {return nil;}
    HZOwnedStockModel *model = pTempArray[indexPath.row];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"code" ofType:@"plist"];
    NSArray *codeArray = [[NSArray alloc] initWithContentsOfFile:path];
    if(codeArray.count>indexPath.row){
        NSDictionary *infoDic = codeArray[indexPath.row];
        model.model_Name = NotNilStr(infoDic[@"name"]);
//        model.model_Code =NotNilStr(infoDic[@"code"]);
    }else{
        NSDictionary *infoDic = codeArray[0];
        model.model_Name = NotNilStr(infoDic[@"name"]);
//        model.model_Code =NotNilStr(infoDic[@"code"]);
    }
    HZStockListTableViewCell *cell = [HZStockListTableViewCell ownedStockCellTableView:tableView indexPath:indexPath cellModel:model cellHight:kListCellH];
    
    return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kListCellH;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSArray *models = _pListData[indexPath.section];
    HZOwnedStockModel *model = models[indexPath.row];
    [self.parent upToParentWithObjc:self withType:kHZ_jumpSingleStock_Action withParamter:model];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kHeaderViewH;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HZHeaderofSectionView *pHeaderView = self.pHeaderViewArray[section];
    pHeaderView.frame = CGRectMake(0, 0, self.frame.size.width, kHeaderViewH);
    return pHeaderView;
}

- (void)upToParentWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter {
    switch (type) {
        case kHZ_ClickMarketSectionHeader_Action:
        case kHZ_stockListMore_Action:
        {
            [self.parent upToParentWithObjc:objc withType:type withParamter:paramter];
        }
            break;
        default:
            break;
    }
}


@end
